package palmtop.com.palmtop.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;

import net.grandcentrix.tray.AppPreferences;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import palmtop.com.palmtop.R;
import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.NetUtil;

public class NotificationService extends Service {

    Context context ;
    String ssgrid;
    String username;
    String userid;
    String role_id;
    MediaPlayer mMediaPlayer = new MediaPlayer();
    private PowerManager.WakeLock wakeLock1 = null;


    public NotificationService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        PowerManager pm1 = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock1 = pm1.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NotificationService.class.getName());
        wakeLock1.acquire();
        this.context = this;
        AppPreferences appPreferences = new AppPreferences(context);
        userid = appPreferences.getString("userid", "");
        username = appPreferences.getString("name", "");
        ssgrid =appPreferences.getString("ssgrid", "");
        role_id = appPreferences.getString("role_id", "");
        checkVersion();
    }
    Disposable mDisposable;
    public void closeTimer(){
        if (mDisposable != null) {
            mDisposable.dispose();
        }
    }

    @Override
    public void onDestroy() {
        if (wakeLock1 != null) {
            wakeLock1.release();
            wakeLock1 = null;
        }
        /*Intent intent = new Intent("com.example.demo.destroy");
        sendBroadcast(intent);*/
        super.onDestroy();
        closeTimer();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

    public void checkVersion(){
        Observable.interval(0,10, TimeUnit.SECONDS)
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mDisposable = d;
                    }

                    @Override
                    public void onNext(Long value) {
                        Log.e("发起请求","发起");
                        selectNoByUserid();
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    private void selectNoByUserid() {
        RequestParams params = new RequestParams(Config.SERVER_URL + "taskapp/selectNoByUserid");

        //73006b03b7f447bf953d065462e5b1ac
        if(role_id.equals("73006b03b7f447bf953d065462e5b1ac")){
            params.addBodyParameter("userid", "73006b03b7f447bf953d065462e5b1ac");
        }
        if(role_id.equals("d18ac0ef29ee477f8e3dd214245a1de5")){
            params.addBodyParameter("userid", ssgrid);
        }
        if(role_id.equals("1f515845ed214961aef8a0bc6646cf2d")){
            params.addBodyParameter("userid", userid);
        }
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if(result.equals("having")){
                    try{
                        mMediaPlayer.reset();
                        mMediaPlayer.setDataSource(Config.base_url+"/palmtop/notification/notification.mp3");
                        mMediaPlayer.prepare();
                        mMediaPlayer.start();

//                        AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.notification);
//                        mMediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(),
//                                file.getLength());
//                        mMediaPlayer.prepare();
//                        file.close();
//                        mMediaPlayer.setVolume(0.5f, 0.5f);
//                        mMediaPlayer.start();
                        updateNoByUserid();
                    }catch (Exception ex){}

                }else{
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });

    }

    private void updateNoByUserid(){
        RequestParams params = new RequestParams(Config.SERVER_URL + "taskapp/updateNoByUserid");
        if(role_id.equals("73006b03b7f447bf953d065462e5b1ac")){
            params.addBodyParameter("userid", "73006b03b7f447bf953d065462e5b1ac");
        }
        if(role_id.equals("d18ac0ef29ee477f8e3dd214245a1de5")){
            params.addBodyParameter("userid", ssgrid);
        }
        if(role_id.equals("1f515845ed214961aef8a0bc6646cf2d")){
            params.addBodyParameter("userid", userid);
        }
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });
    }
}
